﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;

namespace WpfBeyond.pages
{
    /// <summary>
    /// PageBroadcast.xaml 的交互逻辑
    /// </summary>
    public partial class PageBroadcast : Page
    {
        private DispatcherTimer _Timer = null;


        public PageBroadcast()
        {
            InitializeComponent();

            ebm_class_list p_ebm_class_list = new ebm_class_list(cmb_ebmclass);
            ebm_level_list p_ebm_level_list = new ebm_level_list(cmb_ebmlevel);
            ebm_source_list p_ebm_source_list = new ebm_source_list(cmb_source);
            play_mode_list p_play_mode_list = new play_mode_list(cmb_playmode);

            cmb_devicelist.ItemsSource = Common.audio_device_list;
            cmb_devicelist.DisplayMemberPath = "Name";//显示出来的值
            cmb_devicelist.SelectedValuePath = "Value";//实际选中后获取的结果的值

            grid_dev.ItemsSource = Devices.list;

            if (_Timer == null)
            {
                _Timer = new DispatcherTimer();//定时器 ;
                _Timer.Tick += new EventHandler(timeCycle);
                _Timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            }

            _Timer.Start();
        }

        public void timeCycle(object sender, EventArgs e)
        {
            try
            {
                if (Common.reflash_device)
                {
                    grid_dev.ItemsSource = null;
                    grid_dev.ItemsSource = Devices.list;

                    Common.reflash_device = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Bc_Click(object sender, RoutedEventArgs e)
        {
            bool HaveCheck = false;

            for (int i = 0; i < Devices.list.Count; i++)
            {
                if (Devices.list[i].IsCheck == true)
                {
                    HaveCheck = true;
                }
            }

            if (HaveCheck == false)
            {
                Common.ShowMessage("提示", "请选择终端！");
                return;
            }

            TaskInfo t = new TaskInfo();
            t.GenEBMID();//生成EBMID

            t.Bc_type = Convert.ToByte(cmb_ebmclass.SelectedValue);
            t.Event_level = Convert.ToByte(cmb_ebmlevel.SelectedValue);

            t.Volume = Convert.ToByte(tbx.Text);

            if (cmb_playmode.Text == "定时播放")
            {
                t.Sel_dates.Clear();
                foreach (DateTimeRange time in Common.sel_dates)
                {
                    t.Sel_dates.Add(time);
                }

                if (t.Sel_dates.Count == 0)
                {
                    Common.ShowMessage("提示", "请选择定时时段！");
                    return;
                }
            }

            string rtp = TaskFactory.GetRtp();
            t.Rtp = rtp;

            for (int i = 0; i < Devices.list.Count; i++)
            {
                if (Devices.list[i].IsCheck == true)
                {
                    if (Devices.list[i].socket == null)
                    {
                        Common.ShowMessage("提示", "终端连接异常！请检查线路。");
                        continue;
                    }

                    //添加任务相关客户端SOCKET
                    t.Clients.Add(Devices.list[i].serialNo);
                    //设备也添加相关的任务，相互关联
                    //devices[i].tasklist.Add_TaskInfo(t);
                }
            }

            if (cmb_source.SelectedIndex == 0)
            {
                t.Audiopath = cmb_devicelist.SelectedValue.ToString();
                t.Source = 0;
                t.Content = "麦克风";
                t.Mic_vol = double.Parse(tbx_mic_vol.Text) / 100;
            }
            else if (cmb_source.SelectedIndex == 1)
            {
                t.Source = 1;

                foreach (PlayInfo p in Common.sel_playlist)
                {
                    t.Playlist.Add(p);
                }

                if (t.Playlist.Count == 0)
                {
                    Common.ShowMessage("提示", "请选择音频！");
                    return;
                }

                t.Aud_vol = double.Parse(tbx_aud_vol.Text) / 100;
            }
            else
            {
                t.Source = 2;

                t.Audiopath = cmb_devicelist.SelectedValue.ToString();

                foreach (PlayInfo p in Common.sel_playlist)
                {
                    t.Playlist.Add(p);
                }

                if (t.Playlist.Count == 0)
                {
                    Common.ShowMessage("提示", "请选择音频！");
                    return;
                }

                t.Mic_vol = double.Parse(tbx_mic_vol.Text) / 100;
                t.Aud_vol = double.Parse(tbx_aud_vol.Text) / 100;
            }

            TaskFactory.Add_TaskInfo(t);

            if (cmb_playmode.Text == "立即播放")
            {
                t.Start();
                Common.ShowMessage("提示", "播放广播成功");
            }
            else
            {
                Common.ShowMessage("提示", "添加定时广播成功");
            }


        }

        private void DeviceEditClick(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;

            if (btn == null) return;

            string sn = btn.Tag.ToString();

            Device d = Devices.Get_Device(sn);

            WindowDevice wd = new WindowDevice(d);
            wd.ShowDialog();
        }

        private void datetime_Click(object sender, RoutedEventArgs e)
        {
            WindowCalendar win_cal = new WindowCalendar(Common.sel_dates);

            if (true == win_cal.ShowDialog())
            {
                Common.sel_dates.Clear();
                foreach (DateTimeRange r in win_cal.Range)
                {
                    Common.sel_dates.Add(r);
                }

                txt_datetime.Text = Common.sel_dates.Count + " 个时间段";
            }
        }

        private void cmb_playmode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmb_playmode.SelectedIndex == 0)
            {
                panel_datetime.Visibility = Visibility.Collapsed;
            }
            else
            {
                panel_datetime.Visibility = Visibility.Visible;
            }
        }

        private void cmb_source_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmb_source.SelectedIndex == 0)
            {
                panel_device.Visibility = Visibility.Visible;
                panel_mic_vol.Visibility = Visibility.Visible;

                panel_file.Visibility = Visibility.Collapsed;
                panel_aud_vol.Visibility = Visibility.Collapsed;
            }
            else if (cmb_source.SelectedIndex == 1)
            {
                panel_device.Visibility = Visibility.Collapsed;
                panel_mic_vol.Visibility = Visibility.Collapsed;

                panel_file.Visibility = Visibility.Visible;
                panel_aud_vol.Visibility = Visibility.Visible;
            }
            else
            {
                panel_device.Visibility = Visibility.Visible;
                panel_mic_vol.Visibility = Visibility.Visible;

                panel_file.Visibility = Visibility.Visible;
                panel_aud_vol.Visibility = Visibility.Visible;
            }
        }

        private void File_Click(object sender, RoutedEventArgs e)
        {
            WindowPlayList win = new WindowPlayList(Common.sel_playlist);
            if (true == win.ShowDialog())
            {
                txt_File.Text = Common.sel_playlist.Count + " 个文件";
            }
        }

        private async void DelClick(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;

            if (btn == null) return;

            string sn = btn.Tag.ToString();

            Device d = Devices.Get_Device(sn);

            if (d != null)
            {   
                MetroWindow metroWindow = Application.Current.MainWindow as MetroWindow;
                //metroWindow.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Accented; // set the theme                

                MessageDialogResult result = await metroWindow.ShowMessageAsync("提示", "系统还有未结束的广播任务！要全部停止吗？", MessageDialogStyle.AffirmativeAndNegative);
                if (result == MessageDialogResult.Affirmative)
                {
                    Devices.Remove_Devices(d);
                    grid_dev.ItemsSource = null;
                    grid_dev.ItemsSource = Devices.list;
                }
                else
                {

                }
            }
        }
    }
}
